package com.bida.controller.user;


import com.bida.constant.JwtClaimsConstant;
import com.bida.domain.dto.UserLoginDTO;
import com.bida.domain.po.User;
import com.bida.domain.vo.UserLoginVO;
import com.bida.properties.JwtProperties;
import com.bida.properties.WeChatProperties;
import com.bida.result.Result;
import com.bida.service.IUserService;
import com.bida.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author ly
 * @since 2024-09-07
 */
@RestController
@RequestMapping("/user/user")
@Slf4j
public class UserController {


    @Resource
    private IUserService userService;

    @Resource
    private JwtProperties jwtProperties;

    /**
     * 用户 微信登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> wxUserLogin(@RequestBody UserLoginDTO userLoginDTO){
        log.info("微信登录请求参数：{}",userLoginDTO);
        User user = userService.wxUserLogin(userLoginDTO);
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        UserLoginVO userLoginVO = new UserLoginVO();
        userLoginVO.setToken(token);
        userLoginVO.setOpenid(user.getOpenid());
        userLoginVO.setId(user.getId());


        return Result.success(userLoginVO);
    }




}
